﻿
// Edit By: Arthur.li
// Edit At: 2009年4月29日 8:55:51
// Bug Fix

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.Persistent;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;
//本页是对数据字典条目的维护页，不对数据字典本身维护，只对字典中的内容做维护
public partial class System_Dictionary_DictionaryList : PageBase
{
    #region 属性变量 
    /// <summary>
    /// 是否是添加操作
    /// </summary>
    public bool IsAdd
    {
        get { return ViewState["IsAdd"] == null ? true : Convert.ToBoolean(ViewState["IsAdd"].ToString()); }
        set { ViewState["IsAdd"] = value == null ? true :  value; }
    }
    /// <summary>
    /// list中的ID
    /// </summary>
    public decimal DictID
    {
        get { return listDic.SelectedValue==null || listDic.SelectedValue.ToString() == "" ? -1 : Decimal.Parse(listDic.SelectedValue.ToString()); }
    }
    /// <summary>
    /// list中的ID
    /// </summary>
    public decimal ItemID
    {
        get { return listItem.SelectedValue == null | listItem.SelectedValue.ToString() == "" ? -1 : Decimal.Parse(listItem.SelectedValue.ToString()); }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        this.chkLock.Attributes.Add("onclick", "if(this.checked){return confirm('确认锁定吗？');};");
        if (!IsPostBack)
        {
            this.detail.Visible = false;
            BindDataDict();
        }
    }

    #region 数据绑定
    /// <summary>
    /// 绑定Dictionary的数据
    /// </summary>
    protected void BindDataDict()
    {
        DictionaryItemsManager dictmanager = new DictionaryItemsManager();
        this.listDic.Items.Clear();
        ObjectTable ot = dictmanager.GetCourseTable( MyStateManager.DomainID);
        foreach (ObjectRow or in ot)
        {
            Dictionary dict = (Dictionary)or;
            this.listDic.Items.Add(new ListItem(dict.Name + " - " + dict.Code, dict.DictionaryID.ToString()));
        }
    }
    /// <summary>
    /// 绑定Items的数据
    /// </summary>
    /// <param name="dictID"></param>
    protected void BindDataItem(decimal dictID)
    {
        DictionaryItemsManager dictitemmanager = new DictionaryItemsManager();
        this.listItem.Items.Clear();
        ObjectTable ot = dictitemmanager.GetCourseTable(dictID, MyStateManager.DomainID);
        foreach(ObjectRow or in ot)
        {
            DictionaryItems item = (DictionaryItems)or;
            string strtext ="";
            if(item.Active) //有效或无效的显示
            {
                strtext += "√ ";
            }
            else
            {
                strtext += "× ";
            }
            strtext += item.Name + " - " + item.Code ;//显示文本控制
            this.listItem.Items.Add(new ListItem(strtext,item.DictionaryItemID.ToString()));
        }
    }  
    /// <summary>
    /// 绑定ITEM详细内容
    /// </summary>
    protected void BindDataDetail()
    {
        if (ItemID >=0 && !IsAdd)
        {
            DictionaryItemsManager itemmanager = new DictionaryItemsManager();
            DictionaryItems item = itemmanager.Load(ItemID);
            this.txtCode.Text = item.Code;
            this.txtName.Text = item.Name;
            this.chkIsActive.Checked = item.Active;
            this.chkLock.Checked = item.Locked; //锁定的话 所有按钮有效性都要跟着变化
            btnSave.Enabled = !item.Locked;
            this.txtCode.Enabled = !item.Locked;
            this.txtName.Enabled = !item.Locked;
            this.chkIsActive.Enabled = !item.Locked;
            this.chkLock.Enabled = !item.Locked;
        }
        else if (IsAdd)
        {
            this.txtCode.Enabled = true;
            this.txtName.Enabled = true;
            this.chkIsActive.Enabled = true;
            this.chkLock.Enabled = true;
            this.btnSave.Enabled = true;
            this.txtCode.Text = "";
            this.txtName.Text = "";
            this.chkLock.Checked = false;
            this.chkIsActive.Checked = true;
        }
    }

    #endregion

    #region 编辑修改内容
    //添加按钮
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        this.detail.Visible = true;
        IsAdd = true;  
      
        BindDataDetail();
    }
    //编辑按钮
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        this.detail.Visible = true;
        IsAdd = false; 
        BindDataDetail();
    }
    //保存
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!Page.IsValid)
            return;
        
        DictionaryItemsManager itemmanager = new DictionaryItemsManager();
        DictionaryItems item;
        string logcontent = "";
        if (ItemID>=0 && !IsAdd)//更新必须有选ITEM 再取出原来的 
        {
            item = itemmanager.Load(ItemID);
            logcontent += "<b>修改前条目信息</b><br /> ";
            logcontent += " 所属字典:" + item.DictionaryID.ToString();
            logcontent += " 条目名称:" + item.Name;
            logcontent += " 条目代码:" + item.Code;
            logcontent += " 有效:" + item.Active.ToString();
            logcontent += " 锁定:" + item.Locked.ToString();
          
        }
        else if (DictID >= 0 && IsAdd) //添加必须有选分类
        {
            item = new DictionaryItems();
            item.DictionaryID = DictID; 
        }
        else
        {
            return;
        }
        item.Name = txtName.Text;
        item.Code = txtCode.Text;
        item.Active = chkIsActive.Checked;
        item.LAST_UPDATER = MyStateManager.UserID.ToString();
        item.LAST_UPDATE_TIME = BusinessLogic.GetDateTimeDecimal(DateTime.Now);
        item.Locked = chkLock.Checked;
        item.DOMAINID = MyStateManager.DomainID;
        try
        {
            itemmanager.Save(item);

        }
        catch (Exception ex)
        {
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
                Jumpton.SFA.Server.BusinessRule.System.LogType.GENERAL_EXCEPTION, this.MyStateManager.Name,
                this.MyStateManager.UserID, "字典保存 - 异常:" + ex.Message);
            this.ShowMessage(ex.Message);
            return;
        }
        #region 日志
        if (IsAdd)
        {
            logcontent += "<br /><b>新增条目信息:</b> 所属字典ID:" + item.DictionaryID.ToString() ;
            logcontent += " 条目名称:" +item.Name;
            logcontent += " 条目代码:"+item.Code;
            logcontent += " 有效:"+item.Active.ToString();
            logcontent += " 锁定:"+item.Locked.ToString();
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(Jumpton.SFA.Server.BusinessRule.System.LogType.DICTIONARY,
                MyStateManager.Name, MyStateManager.UserID.ToString(), "字典条目添加 - 条目编码:" + item.Code, logcontent);
        }
        else
        {
            logcontent += "<br />修改后条目信息:<br />";
            logcontent += " 所属字典ID:" + item.DictionaryID.ToString();
            logcontent += " 条目名称:" + item.Name;
            logcontent += " 条目代码:" + item.Code;
            logcontent += " 有效:" + item.Active.ToString();
            logcontent += " 锁定:" + item.Locked.ToString();
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(Jumpton.SFA.Server.BusinessRule.System.LogType.DICTIONARY,
               MyStateManager.Name, MyStateManager.UserID.ToString(), "字典条目编辑 - 条目编码:" + item.Code, logcontent);
        }
        #endregion
        //if (IsAdd)
        //{
            this.BindDataItem(DictID);
        //}
        if (chkContinue.Checked) //继续添加
        {
            this.txtCode.Text = "";
            this.txtName.Text = "";
            this.chkLock.Checked = false;
            this.chkIsActive.Checked = true;
        }
        else
        {
            this.detail.Visible = false;
            this.ShowMessage("保存成功");
        }

    }

    /// <summary>
    /// LIST选择
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void listDic_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DictID >= 0)
        {
            BindDataItem(DictID);
        }
    }
    //取消按钮
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        this.detail.Visible = false;
    }
    #endregion
 
}
